/* 1videoConference -- An open source video conferencing platform.
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Data;
using System.IO;

namespace ZipInfo
{
    public class CliZipInfoSub
    {
        NetPeerService.NetPeerServer ZipInfoSubServer = null;

        NetPeerService.NetPeerClient npcZipInfoSub = null;
        object objZipInfoSub = new CsvcZipInfoSub();
        IsvcZipInfoSub ZipInfoSub;

        System.Threading.Thread servClient;

        public string UserName = "";

        string[] strOrgFileList = null;
        List<string> RequiredFiles = new List<string>();
        List<string> SendingFiles = new List<string>();
        Stream stream;
        byte[] BtArr = new byte[5000];
        byte[] BtArrLong = null;
        string MyMeetingID = "";

        public CliZipInfoSub(string MyServiceURI, string UName, string MeetingID)
        {
            lock (this)
            {
                try
                {
                    MyMeetingID = MeetingID;
                    UserName = UName;
                    servClient = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(RegClient));
                    ZipInfoSubServer = new NetPeerService.NetPeerServer(MyServiceURI);
                    ZipInfoSubServer.AddEndPoint(MyServiceURI);
                    ZipInfoSubServer.OpenServer();
                    servClient.Start(MyServiceURI);
                }
                catch (Exception exp)
                {
                    exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--CliZipInfoSub.cs--:--CliZipInfoSub()--:--");
                    Adiance.VMukti.BAL.clsUtility.LogException = exp;
                    //MessageBox.Show(exp.Message);
                }
            }
        }


        void RegClient(object objMyServiceURI)
        {
            try
            {
                lock (this)
                {
                    ((CsvcZipInfoSub)objZipInfoSub).EntsvcJoin += new CsvcZipInfoSub.DelsvcJoin(CliZipInfoSub_EntsvcJoin);
                    ((CsvcZipInfoSub)objZipInfoSub).EntsvcZipFileInfo += new CsvcZipInfoSub.DelsvcZipFileInfo(CliZipInfoSub_EntsvcZipFileInfo);
                    ((CsvcZipInfoSub)objZipInfoSub).EntsvcSetZipName += new CsvcZipInfoSub.DelsvcSetZipName(CliZipInfoSub_EntsvcSetZipName);
                    ((CsvcZipInfoSub)objZipInfoSub).EntsvcSendZipFile += new CsvcZipInfoSub.DelsvcSendZipFile(CliZipInfoSub_EntsvcSendZipFile);
                    ((CsvcZipInfoSub)objZipInfoSub).EntsvcSaveZipFile += new CsvcZipInfoSub.DelsvcSaveZipFile(CliZipInfoSub_EntsvcSaveZipFile);
                    ((CsvcZipInfoSub)objZipInfoSub).EntsvcUnjoin += new CsvcZipInfoSub.DelsvcUnjoin(CliZipInfoSub_EntsvcUnjoin);
                    ((CsvcZipInfoSub)objZipInfoSub).EntsvcServerFileList += new CsvcZipInfoSub.DelsvcServerFileList(CliZipInfoSub_EntsvcServerFileList);

                    npcZipInfoSub = new NetPeerService.NetPeerClient();
                    ZipInfoSub = (IsvcZipInfoSub)npcZipInfoSub.OpenClient<IsvcZipInfoSub>(objMyServiceURI.ToString(), "ZipInfoSubMeshM" + MyMeetingID + "U" + UserName, ref objZipInfoSub);
                    ZipInfoSub.svcJoin();
                }
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--CliZipInfoSub.cs--:--RegClient()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //MessageBox.Show("RegClient--zipinfosubserver" + exp.Message);
            }


        }

        void CliZipInfoSub_EntsvcJoin()
        {
            
        }

        void CliZipInfoSub_EntsvcZipFileInfo(string[] fileList)
        {
            strOrgFileList = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "Zip Files");
            //MessageBox.Show(AppDomain.CurrentDomain.BaseDirectory + "Zip Files");
            for (int i = 0; i < strOrgFileList.Length; i++)
            {
                string[] strFname = strOrgFileList[i].Split('\\');
                stream = new FileStream(strOrgFileList[i], FileMode.Open, FileAccess.Read);
                RequiredFiles.Add(strFname[strFname.Length - 1].ToString() + ":" + stream.Length.ToString());
                stream.Close();
            }

            ZipInfoSub.svcServerFileList(RequiredFiles.ToArray());
            SendingFiles = new List<string>(RequiredFiles.Count);

            if (fileList[0] == "None" || RequiredFiles.Count> fileList.Length)
            {
                for (int i = 0; i < RequiredFiles.Count; i++)
                { 
                    SendingFiles.Add("Send All"); 
                }
                svcSendFile(RequiredFiles.ToArray());
            }
            
            else if (RequiredFiles.Count != fileList.Length)
            {
                for (int i = 0; i < RequiredFiles.Count; i++)
                {
                    SendingFiles.Add("send All");
                }
                svcSendFile(RequiredFiles.ToArray());
            }


            else
            {
                for (int i = 0; i < RequiredFiles.Count; i++)
                {
                    if (RequiredFiles[i] != fileList[i])
                    { 
                        SendingFiles.Add(RequiredFiles[i]); 
                    }
                    else
                    {
                        SendingFiles.Add("Null"); 
                    }
                }
                svcSendFile(SendingFiles.ToArray());
            }
        }

        void CliZipInfoSub_EntsvcSetZipName(string zipfileName)
        {

        }

        void CliZipInfoSub_EntsvcSendZipFile(byte[] zipfile)
        {

        }

        void CliZipInfoSub_EntsvcSaveZipFile()
        {

        }

        void CliZipInfoSub_EntsvcUnjoin()
        {

        }

        void CliZipInfoSub_EntsvcServerFileList(string[] strServerFileInfo)
        { }


        public void svcSendFile(string[] FileName)
        {
            lock (this)
            {
                try
                {
                    for (int i = 0; i < RequiredFiles.Count; i++)
                    {
                        if (FileName[i] != "Null")
                        {
                            stream = new FileStream(strOrgFileList[i], System.IO.FileMode.Open, System.IO.FileAccess.Read);
                            ZipInfoSub.svcSetZipName(FileName[i]);
                            if (stream.Length > 5000)
                            {
                                int k;
                                for (k = 0; k < stream.Length / 5000; k++)
                                {
                                    stream.Read(BtArr, 0, 5000);
                                    ZipInfoSub.svcSendZipFile(BtArr);
                                }

                                if (k * 5000 != stream.Length)
                                {
                                    BtArrLong = new byte[int.Parse(stream.Length.ToString()) - (k * 5000)];
                                    stream.Read(BtArrLong, 0, int.Parse(stream.Length.ToString()) - (k * 5000));
                                    ZipInfoSub.svcSendZipFile(BtArrLong);
                                }
                            }
                            else
                            {
                                BtArrLong = new byte[int.Parse(stream.Length.ToString())];
                                stream.Read(BtArrLong, 0, int.Parse(stream.Length.ToString()));
                                ZipInfoSub.svcSendZipFile(BtArrLong);
                            }
                            ZipInfoSub.svcSaveZipFile();
                            stream.Close();
                        }
                    }
                    ZipInfoSub.svcUnjoin();
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);                   
                    ex.Data.Add("My Key", "Adiance.VMukti.BAL--:--CliZipInfoSub.cs--:--svcSendFile()--:--");
                    Adiance.VMukti.BAL.clsUtility.LogException = ex;
                }
            }
        }

        public void CloseService()
        {
            //ZipInfoSub.svcUnjoin();
            npcZipInfoSub.CloseClient<IsvcZipInfoSub>();
            ZipInfoSubServer.CloseServer();
        }
    }
}
